#version 400 core

#define PARTICLE_POSITION    0
#define PARTICLE_VEL		 1
#define PARTICLE_COLOR       2
#define PARTICLE_PROP        3

uniform mat4 view;
uniform mat4 proj;

uniform vec2 screenSize;

layout(location = PARTICLE_POSITION) in vec4 pos;
layout(location = PARTICLE_VEL) in vec3 vel;
layout(location = PARTICLE_COLOR) in vec4 color;
layout(location = PARTICLE_PROP) in vec4 props;

out vec4 fColor;
flat out vec4 fPosFlat;
flat out vec4 fProps;

void main()
{
	vec4 eyePos = view * vec4(pos.xyz, 1.0);
	vec4 projVoxel = proj * vec4(props.z, props.z, eyePos.z, eyePos.w);
	vec2 projSize = screenSize * projVoxel.xy / projVoxel.w;
	gl_PointSize = 0.25 * (projSize.x+projSize.y);
	vec4 pos = proj * eyePos;
	gl_Position = pos;
	fProps = props;

	fProps.y = clamp(1.0 - fProps.y/fProps.x, 0.0, 1.0); // 1.0: born, 0.0: dead 

	if (abs(fProps.w-1.0) < 0.1) 
		fProps.y *= fProps.y; //out_quadratic
	else if (abs(fProps.w-2.0) < 0.1) 
		fProps.y = (0.5 - abs(0.5-fProps.y))*2.0f; //inout_linear
	else if (abs(fProps.w-3.0) < 0.1) 
		fProps.y = (0.25-(fProps.y-0.5)*(fProps.y-0.5))*4.0; //inout_quadratic

	fColor = color;
}